package eteam.aps.config;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import eteam.aps.common.anno.ExcelExport;
import eteam.aps.common.exceptions.BusinessException;
import eteam.aps.common.helper.Helper;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.Collections;

/**
 * @author Ll
 * @since 2024/1/17
 */
@Aspect
@Component
public class ExcelAdvance {

    @Resource
    private HttpServletResponse response;

    @Pointcut("@annotation(eteam.aps.common.anno.ExcelExport)")
    public void excelExportPoint() {

    }

    @Around("excelExportPoint()")
    public Object exportAdvance(ProceedingJoinPoint pjp) throws Throwable {
        Object proceed = pjp.proceed();
        if (proceed instanceof Collection) {
            if (((Collection<?>) proceed).isEmpty()) throw new BusinessException("暂无数据！");
            MethodSignature signature = (MethodSignature) pjp.getSignature();
            ExcelExport export = signature.getMethod().getAnnotation(ExcelExport.class);
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode(export.fileName(), "UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
            String sheet = export.sheet();
            if (Helper.isNotEmpty(sheet)) sheet = export.fileName();
            EasyExcel.write(response.getOutputStream(), export.head()).registerWriteHandler(getStyle()).sheet(sheet).doWrite((Collection<?>) proceed);
            return Collections.emptyList();
        }
        throw new BusinessException("导出失败！");
    }

    /**
     * 样式
     */
    public static HorizontalCellStyleStrategy getStyle() {
        // 头的策略
        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        // 字体大小
        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        WriteFont contentWriteFont = new WriteFont();
        contentWriteFont.setFontHeightInPoints((short) 11);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
        return new HorizontalCellStyleStrategy(new WriteCellStyle(), contentWriteCellStyle);
    }
}
